package com.jms.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * create by yongli on 2021-06-05 23:35
 */
@Configuration
@MapperScan(basePackages = "com.jms.mapper", sqlSessionFactoryRef = "localSqlSessionFactory")
public class LocalSqlSessionFactoryConfig {

    // 看下这个对象时否是系统注入了
    @Autowired
    @Qualifier("localDataSource")
    DataSource localDatasource;


    // 方法名不重要,base中的name属性很重要
    @Bean(name = "localSqlSessionFactory")
    public SqlSessionFactoryBean getLocalSqlSessionFactory() {
        // SqlSessionFactory本身没有去读取数据库配置的代码，需要手动或者spring-boot的方式
        // 这是纯mybatis的用法
        // SqlSessionFactory factoryBean = new SqlSessionFactoryBuilder().build(configuration)
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(localDatasource);

        return factoryBean;
    }
}
